package com.yupi.springbootinit.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.springbootinit.constant.CommonConstant;
import com.yupi.springbootinit.model.dto.chart.ChartQueryRequest;
import com.yupi.springbootinit.model.vo.ChartVO;
import com.yupi.springbootinit.utils.SqlUtils;
import com.yupi.springbootinit.mapper.ChartMapper;
import com.yupi.springbootinit.model.entity.Chart;
import com.yupi.springbootinit.service.ChartService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 20662
* @description 针对表【chart(图表信息表)】的数据库操作Service实现
* @createDate 2024-01-18 18:57:00
*/
@Service
public class ChartServiceImpl extends ServiceImpl<ChartMapper, Chart>
    implements ChartService {

    @Override
    public Page<ChartVO> getChartVOPage(Page<Chart> chartPage, HttpServletRequest request) {

        //todo 需要思考怎么写转换
        return null;
    }

    /**
     * 根据id查询VO封装对象
     *
     * @param id
     * @return
     */
    @Override
    public ChartVO getOneChart(long id) {
        QueryWrapper<Chart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        Chart one = this.getOne(queryWrapper);
        ChartVO chartVO = ChartVO.objToVo(one);
        return chartVO;
    }

    @Override
    public Page<ChartVO> pageVO(ChartQueryRequest chartQueryRequest) {
        QueryWrapper<Chart> chartQueryWrapper = new QueryWrapper<>();
        if (chartQueryRequest == null) {
            return new Page<>();
        }
        Long id = chartQueryRequest.getId();
        String goal = chartQueryRequest.getGoal();
        String chartType = chartQueryRequest.getChartType();
        Long userId = chartQueryRequest.getUserId();
        String sortField = chartQueryRequest.getSortField();
        String sortOrder = chartQueryRequest.getSortOrder();

        // 拼接查询条件
        if (id != null && id > 0) {
            chartQueryWrapper.eq("id", id);
        }
        if (StringUtils.isNotBlank(goal)) {
            chartQueryWrapper.like("goal", goal);
        }
        if (StringUtils.isNotBlank(chartType)){
            chartQueryWrapper.like("chartType", chartType);
        }
        if (userId != null && id > 0) {
            chartQueryWrapper.eq("userId", userId);
        }
        //拼接排序条件
        chartQueryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC), sortField);
        Page<Chart> page = this.page(new Page<>(chartQueryRequest.getCurrent(), chartQueryRequest.getPageSize()), chartQueryWrapper);
        List<Chart> records = page.getRecords();
        //把集合中的对象转化为VO
        List<ChartVO> chartVOList = records.stream()
                .map(ChartVO::objToVo)
                .collect(Collectors.toList());
        Page<ChartVO> chartVOPage = new Page<>();
        chartVOPage.setRecords(chartVOList);
        return chartVOPage;
    }

    /**
     * 获取查询包装类
     *
     * @param chartQueryRequest
     * @return
     */
    @Override
    public QueryWrapper<Chart> getQueryWrapper(ChartQueryRequest chartQueryRequest) {
        QueryWrapper<Chart> chartQueryWrapper = new QueryWrapper<>();
        if (chartQueryRequest == null) {
            return chartQueryWrapper;
        }
        Long id = chartQueryRequest.getId();
        String name = chartQueryRequest.getName();
        String goal = chartQueryRequest.getGoal();
        String chartType = chartQueryRequest.getChartType();
        Long userId = chartQueryRequest.getUserId();
        String sortField = chartQueryRequest.getSortField();
        String sortOrder = chartQueryRequest.getSortOrder();


        // 拼接查询条件
        if (id != null && id > 0) {
            chartQueryWrapper.eq("id", id);
        }
        if (StringUtils.isNotBlank(goal)) {
            chartQueryWrapper.like("goal", goal);
        }
        if (StringUtils.isNotBlank(chartType)){
            chartQueryWrapper.like("chartType", chartType);
        }
        if (userId != null && userId > 0) {
            chartQueryWrapper.eq("userId", userId);
        }
        //mybaitsplus提供的方法，第一个参数可以直接判断是都为空
        chartQueryWrapper.like(StringUtils.isNotBlank(name), "name", name);

        //拼接排序条件
        chartQueryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC), sortField);
        return chartQueryWrapper;
    }



}




